package com.huitone.smspfm.context.thread;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.huitone.smspfm.context.app.AppManager;

/**   
* @Title: AppCustomThread.java 
* @Package com.huitone.smspfm.app.thread 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月15日 下午2:52:32 
* @version V1.0   
*/
public class AppCustomRunnable implements Runnable {
	
	private static Logger logger = LogManager.getLogger(AppCustomRunnable.class);
	
	private AppCustomRunnableBody runnableBody;
	
	private long initTime = 0;
	
	private long sleepTime = initTime;

	public AppCustomRunnable(AppCustomRunnableBody runnableBody) {
		this.runnableBody = runnableBody;
	}

	@Override
	public void run() {
		
		String threadName = Thread.currentThread().getName();
		
		while(AppManager.isRunning()) {
			
			try {
				
				if (!AppManager.isReloading()) {
					
					AppManager.threadExcuSucc.put(threadName, false);
					
					runnableBody.execute(this);
					
					AppManager.threadExcuSucc.put(threadName, true);
					
					logger.debug("线程[" + threadName + "]正常运行中.");
					
				} else {
					logger.debug("线程[" + threadName + "]进入参数加载模式.");
				}
				
				Thread.currentThread();
				Thread.sleep(sleepTime);
			} catch (Exception e) {
				//logger.error("线程[" + threadName + "]被中断退出:", e);
				break;
			}
		}
		
		AppManager.threadExcuSucc.remove(threadName);
		
		logger.info("线程[" + threadName + "]已退出!");
	}

	public long getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}
	
	public synchronized void increaseSleepTime() {
		
		if (sleepTime < 60000) {
			sleepTime ++;
		}
	}
	
	public synchronized void initSleepTime() {
		sleepTime = initTime;
	}

}
